﻿using HZYProject.Core.Repositories;
using HZYProject.Data;
using HZYProject.Domain;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HZYProject.Service.TestPaperServices
{
    public class TestPaperService : ITestPaperService
    {
        private readonly IRepository<TestPaper, Guid> _repository;
        private readonly IRepository<Question, Guid> _questionrepository;
        private readonly HZYProjectDbContext _dbContext;

        public TestPaperService(HZYProjectDbContext dbContext,IRepository<TestPaper, Guid> repository, IRepository<Question, Guid> questionrepository)
        {
            _repository = repository;
            _questionrepository = questionrepository;
            _dbContext = dbContext;
        }

        public async Task<PagedResultDto<TestPaper>> GetPaginatedResult(GetTestPaperInput input)
        {
            var query = _repository.GetAll();
            //搜索
            if (!string.IsNullOrEmpty(input.Title))
            {
                query = query.Where(x => x.Title.Contains(input.Title));
            }
            if (input.Status > 0)
            {
                query = query.Where(x => x.Status == input.Status);
            }
            //统计查询数据的总条数，用于分页计算总页数
            var count = query.Count();
            query = query.Include("Course");
            //根据需求进行排序，然后进行分页逻辑的计算
            query = query.OrderByDescending(x => x.Title).Skip((input.CurrentPage - 1) * input.MaxResultCount).Take(input.MaxResultCount);
            //将查询结果转换为List集合，加载到内存中
            var models = await query.AsNoTracking().ToListAsync();
            var dtos = new PagedResultDto<TestPaper>
            {
                TotalCount = count,
                CurrentPage = input.CurrentPage,
                MaxResultCount = input.MaxResultCount,
                Data = models,
                FilterText = input.FilterText,
            };
            return dtos;
        }

        /// <summary>
        /// 添加试题
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public GetTestPaperInput AddQuestion(GetTestPaperInput input)
        {
            var list = _questionrepository.GetAll().Where(p => p.CourseID == input.CourseID).ToList();
            var str = "";
            for (int i = 0; i < list.Count; i++)
            {
                if (i >= input.QCount)
                    break;
                else
                    str += list[i].QuestionID + ",";
            }
            str = str.Substring(0, str.Length - 1);
            GetTestPaperInput test = new GetTestPaperInput { Content = str, QCount = list.Count };
            return test;
        }

        public List<Question> GetQuestions(string[] qid)
        {
            List<Question> list = new List<Question>();
            
            foreach (var item in qid)
            {
                Guid questionId = new Guid(item);
                //list.Add(_dbContext.Questions.FirstOrDefault(x => x.QuestionID == questionId));
                list.Add(_questionrepository.FirstOrDefault(x => x.QuestionID == questionId));
            }
            return list;
        }
    }
}
